package com.ljm.boot.retry;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.retry.backoff.FixedBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;

@EnableRetry
@SpringBootApplication
public class RetryApplication {
    public static void main(String[] args) {
        SpringApplication.run(RetryApplication.class, args);
    }

    @Bean
    public RetryTemplate retryTemplate() {
        final SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();
        //设置最多执行次数, 包含第一次执行,下面配置成3，则第一次执行出现异常后最多会再重试2次
        simpleRetryPolicy.setMaxAttempts(3);
        final FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
        //设置重试间隔时间  单位 ms
        fixedBackOffPolicy.setBackOffPeriod(1000L);
        return RetryTemplate.builder()
                .customPolicy(simpleRetryPolicy)
                .customBackoff(fixedBackOffPolicy)
                .build();
    }
}
